package com.tronsis.distribution.activity.me.stock;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.LowerStockBean;
import com.tronsis.distribution.biz.factory.OrderBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * allen: 下级库存
 */
public class LowerStockActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_stock)
    RecyclerView mRvStock;

    private ArrayList<LowerStockBean> mMyStockList = new ArrayList<>();
    private String id;
    private CommonAdapter<LowerStockBean> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lower_stock);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.lower_stock));
        id = getIntent().getStringExtra("id");
        initRv();

        httpGetStockList(true, id);

    }


    private void initRv() {
        adapter = new CommonAdapter<LowerStockBean>(mContext, mMyStockList, R.layout.item_stock) {
            @Override
            protected void convert(ViewHolder holder, LowerStockBean stockBean, int position) {
                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + stockBean.getCoverFid());
                holder.setText(R.id.tv_product_name, stockBean.getName());
                holder.setText(R.id.tv_amount, "x" + stockBean.getCount());
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                // TODO: 2019/4/14  是否要详情
//                Intent intent = new Intent(mContext, GoodsDetailActivity.class);
//                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });


//        mLoadMoreWrapper = new LoadMoreWrapper(adapter);
//        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
//        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                if (mMyStockList.size() % 15 == 0 && mMyStockList.size() > 0 && mMyStockList.size() < mTotal) {
//                    mPage = (mMyStockList.size() / 15) + 1;
//                    httpGetStockList(false, id);
//                }
//            }
//        });
        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvStock.addItemDecoration(decoration);
        mRvStock.setLayoutManager(new LinearLayoutManager(mContext));
        mRvStock.setAdapter(adapter);
    }


    private void httpGetStockList(final boolean isRefresh, String id) {
        try {
            OrderBizFactory.createBiz().getLowerStockList(this, id, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    if(data!=null){
                        List<LowerStockBean> list = (List<LowerStockBean>) data;
                        if (isRefresh) {
                            mMyStockList.clear();
                        }

                        for (LowerStockBean lowerStockBean : list) {
                            mMyStockList.add(lowerStockBean);
                        }
                        adapter.notifyDataSetChanged();
                    }

                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();
        }
    }

}
